<script setup>
import {useRoute,useRouter} from 'vue-router'
import {onMounted,ref,reactive } from 'vue'
import {findById} from '@/api/food'
import {findById as findByelderId} from '@/api/elderInfo'
import {findById as findByCaregiverId} from '@/api/caregiver'
import dayjs from 'dayjs'

const formatTime = (time) => {
  return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
}



const route =useRoute()
const router =useRouter()

onMounted(async () => {
    const id = route.query.id;
    try {
        // 等待第一个请求完成
        const foodRes = await findById(id);
        foodView.value = foodRes.data;
        console.log("----->", foodView.value.elderlyId);
        
        // 使用获取到的elderlyId发起第二个请求
        const elderRes = await findByelderId(foodView.value.elderlyId);

        elderInfoView.value = elderRes.data;
         // 使用获取到的elderlyId发起第二个请求
         const careGiverRes = await findByCaregiverId(foodView.value.caregiverId);
        careGiverView.value = careGiverRes.data;
        
    } catch (error) {
        console.error("请求出错:", error);
    }
});
const foodView =ref({})
const elderInfoView =ref({})
const careGiverView =ref({})
const handleBack=()=>{
    router.back()
}
const mealTypeMap = {
  '0': '常规健康',
  '1': '较健康',
  '2': '健康',
  '3': '非常健康',
  '4': '医疗级健康',
  '5': '极致健康'
};

const getMealTypeName = (mealId) => {
  return mealTypeMap[mealId] || mealId;
};
const getMealStatusName = (status) => {
  const statusMap = {
    '0': '未食用',
    '1': '已食用',
    '2': '部分食用',
    '3': '未提供',
    '4': '异常'
  };
  return statusMap[status] || status;
};
</script>

<template>
  <div class="page-container">
    <el-card class="header-card" shadow="hover">
      <div class="header-content">
        <el-icon class="title-icon"><Document /></el-icon>
        <h1 class="page-title">食品记录详细信息</h1>
        <div class="record-id">记录编号: {{ foodView.recordId }}</div>
      </div>
    </el-card>
    
    <div class="content-container">
      <el-card class="info-card" shadow="hover">
        <div class="card-content">
          <!-- 老人信息 -->
          <div class="info-section">
            <div class="section-header">
              <el-icon><User /></el-icon>
              <h2>老人信息</h2>
            </div>
            <el-divider class="custom-divider" />
            <div class="info-grid">
              <div class="info-item">
                <span class="info-label">老人编号：</span>
                <span class="info-value">{{ foodView.elderlyId }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">姓名：</span>
                <span class="info-value">{{ elderInfoView.name }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">护理人：</span>
                <span class="info-value">{{ careGiverView.caregiverId || '--' }}</span>
              </div>
            </div>
          </div>
          
          <!-- 膳食信息 -->
          <div class="info-section">
            <div class="section-header">
              <el-icon><Food /></el-icon>
              <h2>膳食信息</h2>
            </div>
            <el-divider class="custom-divider" />
            <div class="info-grid">
              <div class="info-item">
                <span class="info-label">膳食模板：</span>
                <el-tag class="meal-tag" effect="dark" :type="{
                  '0': '',
                  '1': 'info',
                  '2': 'success',
                  '3': 'primary',
                  '4': 'warning',
                  '5': 'danger'
                }[foodView.mealId]">
                  {{ getMealTypeName(foodView.mealId) }}
                </el-tag>
              </div>
              <div class="info-item">
                <span class="info-label">用餐时间：</span>
                <span class="info-value">{{ foodView.mealTime }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">用餐状态：</span>
                <el-tag class="status-tag" :type="{
                  '0': 'info',
                  '1': 'success',
                  '2': 'warning',
                  '3': '',
                  '4': 'danger'
                }[foodView.status]">
                  {{ getMealStatusName(foodView.status) }}
                </el-tag>
              </div>
              <div class="info-item full-width">
                <span class="info-label">反馈/备注：</span>
                <span class="info-value">{{ foodView.feedback || '无' }}</span>
              </div>
            </div>
          </div>
          
          <!-- 记录信息 -->
          <div class="info-section">
            <div class="section-header">
              <el-icon><Tickets /></el-icon>
              <h2>记录信息</h2>
            </div>
            <el-divider class="custom-divider" />
            <div class="info-grid">
              
              <div class="info-item">
                <span class="info-label">记录时间：</span>
                <span class="info-value">{{ formatTime(foodView.createTime) }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">更新时间：</span>
                <span class="info-value">{{ formatTime(foodView.updateTime) }}</span>
              </div>
            </div>
          </div>
        </div>
      </el-card>
      
      <!-- 返回按钮 -->
      <div class="action-buttons">
        <el-button type="primary" @click="handleBack" class="back-button">
          <el-icon class="button-icon"><Back /></el-icon>
          返回
        </el-button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.page-container {
  padding: 20px;
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 
               'Microsoft YaHei', Arial, sans-serif;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
  min-height: 100vh;
}

.header-card {
  margin-bottom: 20px;
  border-radius: 12px;
  background: linear-gradient(120deg, #4a6bdf 0%, #3a56c4 100%);
  border: none;
  overflow: hidden;
  box-shadow: 0 6px 18px rgba(65, 105, 225, 0.25);
}

.header-content {
  display: flex;
  align-items: center;
  padding: 20px 30px;
  color: white;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  margin: 0 15px 0 10px;
  color: white;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.title-icon {
  font-size: 28px;
  color: #ffd04b;
  margin-right: 10px;
}

.record-id {
  margin-left: auto;
  font-size: 16px;
  opacity: 0.9;
}

.content-container {
  display: flex;
  flex-direction: column;
}

.info-card {
  border-radius: 12px;
  border: none;
  background: white;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.info-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.12);
}

.card-content {
  padding: 25px 30px;
}

.info-section {
  margin-bottom: 30px;
  padding: 15px 0;
}

.section-header {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.section-header h2 {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  margin-left: 10px;
}

.section-header .el-icon {
  font-size: 22px;
  color: #4a6bdf;
}

.custom-divider {
  margin: 10px 0 20px;
  background-color: #f0f4f8;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px 30px;
}

.info-item {
  display: flex;
  align-items: flex-start;
  padding: 8px 0;
}

.full-width {
  grid-column: 1 / -1;
}

.info-label {
  font-weight: 500;
  color: #5a6d88;
  min-width: 100px;
  text-align: right;
  margin-right: 15px;
}

.info-value {
  font-weight: 500;
  color: #34495e;
  flex: 1;
  line-height: 1.6;
}

.meal-tag {
  font-weight: 600;
  padding: 6px 15px;
  border-radius: 20px;
  font-size: 14px;
}

.status-tag {
  font-weight: 500;
  padding: 5px 12px;
  border-radius: 4px;
}

.action-buttons {
  text-align: center;
  margin-top: 30px;
}

.back-button {
  width: 140px;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(120deg, #4a6bdf 0%, #3a56c4 100%);
  border: none;
  box-shadow: 0 4px 12px rgba(65, 105, 225, 0.3);
  transition: all 0.3s ease;
}

.back-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 15px rgba(65, 105, 225, 0.4);
}

.back-button:active {
  transform: translateY(0);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .record-id {
    margin: 10px 0 0;
    align-self: flex-start;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .info-label {
    min-width: 90px;
    text-align: left;
  }
}
</style>